#!/bin/bash
# EbbFlow AWS Deployment Script

set -e

# Configuration
NODES=${1:-4}
INSTANCE_TYPE=${2:-t3.medium}
DURATION=${3:-60}
REGION=${4:-us-east-1}
AMI_ID="ami-0c02fb55956c7d316"  # Amazon Linux 2
KEY_NAME="ebbflow-aws-key"
SECURITY_GROUP="ebbflow-sg"

echo "=== EbbFlow AWS Deployment ==="
echo "Nodes: $NODES"
echo "Instance Type: $INSTANCE_TYPE"
echo "Duration: $DURATION seconds"
echo "Region: $REGION"

# Create instances
echo "Creating AWS instances..."
for i in $(seq 0 $((NODES-1))); do
    echo "Creating node $i..."
    aws ec2 run-instances \
        --image-id $AMI_ID \
        --instance-type $INSTANCE_TYPE \
        --key-name $KEY_NAME \
        --security-group-ids $SECURITY_GROUP \
        --region $REGION \
        --tag-specifications "ResourceType=instance,Tags=[{Key=Name,Value=ebbflow-node-$i}]" \
        --user-data file://user_data.sh
done

# Wait for instances to be ready
echo "Waiting for instances to be ready..."
sleep 60

# Get instance IPs
echo "Getting instance IPs..."
aws ec2 describe-instances \
    --filters "Name=tag:Name,Values=ebbflow-node-*" \
    --query 'Reservations[*].Instances[*].[PublicIpAddress,Tags[?Key==Name].Value|[0]]' \
    --output table

echo "Deployment completed!"
echo "Use ./test_aws.sh to run tests"
